<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var father = {
      age: 25,
      name: '张三',
      sex: '男',
      hobby: [1, 2, 3, 4],
      say: function () {
        console.log('father');
      },
      son: {
        name: '小明', sex: '男'
      }
    }


    // 封装函数 
    function deepCopy(newobj, oldobj) {
      for (var k in oldobj) {
        // 判断我们的属性值属于那种数据类型
        // 1. 获取属性值  oldobj[k]
        var item = oldobj[k];
        // 2. 判断这个值是否是数组
        if (item instanceof Array) {
          newobj[k] = [];
          deepCopy(newobj[k], item)
        } else if (item instanceof Object) {
          // 3. 判断这个值是否是对象
          newobj[k] = {};
          deepCopy(newobj[k], item)
        } else {
          // 4. 属于简单数据类型
          newobj[k] = item;
        }
      }
    };
    var people = {}
    deepCopy(people, father)
    people.name = '李四'
    people.hobby[1] = 9
    people.say = function () {
      console.log('people');
    }
  </script>
</body>

</html>